package test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;
import java.util.List;
import java.util.Set;

public class test01 {
    public static void test01(Jedis j){
        System.out.println("get 和 set");
        j.set("k1", "11");
        j.set("k2","22");
        SetParams params = new SetParams();
        params.ex(10);//设置过期时间
        params.nx();
        j.set("k1","33",params);
        String val = j.get("k1");
        System.out.println(val);
    }

    public static void test02(Jedis j){
        System.out.println("exists del");
        j.flushAll();
        j.set("k1","11");
        j.set("k2","11");
        j.set("k3","11");
        boolean k1 = j.exists("k1");
        System.out.println("k1:" + k1);
        j.del("k1");
        boolean k2 = j.exists("k1");
        System.out.println("k2: "+k2);
    }

    public static void test03(Jedis j){
        System.out.println("keys:");
        j.flushAll();
        j.mset("k1","11","k2","22","k3","33");
        List<String> mget = j.mget("k1", "k2", "k3");
        System.out.println(mget);
        Set<String> keys = j.keys("*");
        System.out.println(keys);
    }

    public static void test04(Jedis j){
        System.out.println("expire 和 ttl");
        j.flushAll();
        j.set("k1","11");
        j.expire("k1",10);//设置过期时间
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long k1 = j.ttl("k1");
        System.out.println("k1: "+k1);
    }

    public static void test05(Jedis j){
        System.out.println("type: ");
        j.flushAll();

        j.set("k1","11");
        String k1 = j.type("k1");
        System.out.println(k1);

        j.hset("h1","f1","22");
        String h1 = j.type("h1");
        System.out.println(h1);

        j.lpush("l1","11","22","33");
        String l1 = j.type("l1");
        System.out.println(l1);

        j.sadd("s1","11","22","33");
        String s1 = j.type("s1");
        System.out.println(s1);

        j.zadd("z1",1.0,"11");
        String z1 = j.type("z1");
        System.out.println(z1);
    }

    public static void main(String[] args) {
        //连接到redis服务器上:
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis j = jedisPool.getResource()){
            //redis上的各种命令,都对应到jedis上的各个方法
//            test01(j);
//            test02(j);
//            test03(j);
//            test04(j);
            test05(j);
        }
    }
}
